﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quexcel.OpenXML.Interfaces;
using System.Xml;
using System.IO.Packaging;

namespace Quexcel.OpenXML.Excel.SharedString.IO
{
    internal class SharedStringReader : ISharedStringReader
    {
        private XmlDocument sharedStringDoc;
        private PackagePart documentPart;
        protected XmlNamespaceManager nsManager;

        internal SharedStringReader(PackagePart documentPart, XmlNamespaceManager nsManager)
        {
            sharedStringDoc = new XmlDocument();
            sharedStringDoc.Load(documentPart.GetStream());
            this.documentPart = documentPart;
            this.nsManager = nsManager;
        }

        #region IReader Members
        protected int GetSize()
        {
            return sharedStringDoc.SelectNodes("//d:t", nsManager).Count;
        }
        public string ReadSharedString(int index)
        {
            XmlNodeList list = sharedStringDoc.SelectNodes("//d:t", nsManager);
            if (list != null && index < list.Count)
                return list[index].InnerText;
            return null;
        }
        public int CheckSharedString(string data)
        {
            XmlNodeList list = sharedStringDoc.SelectNodes("//d:t", nsManager);
            
            for(int i=0; i<list.Count; i++)
            {
                if(list[i].InnerText == data)
                    return i;
            }
            return -1;
        }
        public void Close()
        {
            sharedStringDoc = null;
        }
        #endregion

        #region IReader Members

        public string Read(string addressName)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
